package com.eos.orm.c3p0.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.eos.common.tool.CoreKeyContext;
import com.eos.orm.c3p0.dao.AbstractC3p0Dao;
import com.eos.orm.c3p0.dao.DBConnectionManager;
import com.eos.orm.mybatis.model.ActivtyMappSales;
import com.hf.utils.StringUtil;

/**
 * sys_activty活动表的分页查询及相关批处理操作
 * 
 * @author sz
 * 
 */
public class ActivtyMappSalesDaoImpl extends AbstractC3p0Dao<ActivtyMappSales> {

	/**
	 * 查询的条件：id/deal_flag/status/customer_id/start_time/end_time
	 */
	public String condition1(Map<String, String> map) {
		StringBuilder condition = new StringBuilder();
		if (null == map)
			return condition.append("1=0").toString();
		if (!StringUtil.isEmpty(map.get("id")))
			condition.append("id='").append(map.get("id")).append("' and ");
		if (!StringUtil.isEmpty(map.get("deal_flag")))
			condition.append("deal_flag='").append(map.get("deal_flag"))
					.append("' and ");
		if (!StringUtil.isEmpty(map.get("status")))
			condition.append("status=").append(map.get("status")).append(
					" and ");
		if (!StringUtil.isEmpty(map.get("mapp_id")))
			condition.append("mapp_id=").append(map.get("mapp_id")).append(
					" and ");
		if (!StringUtil.isEmpty(map.get("sales_id")))
			condition.append("sales_id=").append(map.get("sales_id")).append(
					" and ");
		return condition.append("1=1").toString();
	}

	@Override
	public List<ActivtyMappSales> findPage(Map<String, String> map) {
		List<ActivtyMappSales> templist = new ArrayList<ActivtyMappSales>();
		if (null == map)
			return templist;
		Connection connection = DBConnectionManager.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = StringUtil.replace(StringUtil.replace(StringUtil.replace(
				CoreKeyContext.getCoreKey("tabs_page_sqlscript"),
				"$table_name$", map.get("table_name")), "$condition1$",
				condition1(map)), "$condition2$", condition2(map));
		try {
			pstmt = connection.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				ActivtyMappSales ams = new ActivtyMappSales();
				ams.setComboCycle(rs.getInt("combo_cycle"));
				ams.setComboId(rs.getInt("combo_id"));
				ams.setDealFlag(rs.getString("deal_flag"));
				ams.setId(rs.getInt("id"));
				ams.setInsertTime(rs.getString("insert_time"));
				ams.setInsertUserId(rs.getString("insert_user_id"));
				ams.setIsSubsidy(rs.getString("is_subsidy"));
				ams.setMappId(rs.getInt("mapp_id"));
				ams.setModifyTime(rs.getString("modify_time"));
				ams.setSalesId(rs.getInt("sales_id"));
				ams.setSalesPrice(rs.getString("sales_price"));
				ams.setStatus(rs.getString("status"));
				ams.setUpdateUserId(rs.getString("update_user_id"));
				templist.add(ams);
				ams = null;
			}
		} catch (SQLException e) {
			System.out
					.println("SysActivtyDaoImpl exec findPage operator is fail,Exception is :"
							+ e.getMessage());
		} finally {
			freeResource(connection, pstmt, rs);
		}
		return templist;
	}
}
